#include "morp.h"

void Morphology::init( const char * path )
   {
/*   if( !path )
   	if( !CLemmatizerRussian::rml_path )
	   	throw std::string( "Dictionaries path is not declared" );
	   else
	   ;
	else
	   CLemmatizerRussian::rml_path = path;
*/	   
	prop.init( path );
   LemmatizerRussian = new CLemmatizerRussian;

   if( !LemmatizerRussian->LoadDictionariesRegistry() )
      throw std::string( "Error loading CLemmatizerRussian" );
/*!!*/
   RusGramTab = new CRusGramTab;

   if( !RusGramTab->LoadFromRegistry() )
      throw std::string( "Error loading CRusGramTab" );
#if 1 // !! alexch
   LemmatizerEnglish = new CLemmatizerEnglish;

   if( !LemmatizerEnglish->LoadDictionariesRegistry() )
      throw std::string( "Error loading CLemmatizerEnglish" );
/*!!
   EngGramTab = new CEngGramTab;

   if( !EngGramTab->LoadFromRegistry() )
      throw std::string( "Error loading CEngGramTab" );
*/

   LemmatizerGerman = new CLemmatizerGerman;

   if( !LemmatizerGerman->LoadDictionariesRegistry() )
      throw std::string( "Error loading CLemmatizerGerman" );
/*!!
   GerGramTab = new CGerGramTab;

   if( !GerGramTab->LoadFromRegistry() )
      throw std::string( "Error loading CGerGramTab" );
*/
#endif
   }

std::list<std::string> Morphology::getMorphInfo( CLemmatizer& lem, CAgramtab& Gram, std::string Form ) const
	{
	std::string  Result;
	std::list<std::string>  res;
	std::string 			str;

//	bool bCapital   = is_upper_alpha((BYTE)Form[0], Language);
	bool bCapital = false;

	std::vector<CFormInfo> Paradigms;

	lem.CreateParadigmCollection(false, Form, bCapital, Paradigms);

	for (int i =0; i < Paradigms.size(); i++)
		{
		const CFormInfo& F = Paradigms[i];
		for (int k=0; k < F.GetCount (); k++)
			{
			str = Paradigms[i].GetWordForm(k);
//			Result += str;
			res.push_back(str );
//			Result += " ";
			}
   	}

	return res;   	

	}

std::string Morphology::getMorphInfo( std::string& s, std::string lang ) const
   {
   std::string 			str;
   std::list<std::string>  res;
   std::list<std::string>::iterator p;
   
	if( lang == "rus" )
		res = getMorphInfo( *LemmatizerRussian, *RusGramTab, s );
	else
	if( lang == "eng" )
		res = getMorphInfo( *LemmatizerEnglish, *EngGramTab, s );
	else
	if( lang == "ger" )
		res = getMorphInfo( *LemmatizerGerman, *GerGramTab, s );
	else
		{
		res = getMorphInfo( *LemmatizerEnglish, *EngGramTab, s );
		if( res.size() <= 1 )
			{
			res = getMorphInfo( *LemmatizerGerman, *GerGramTab, s );
			if( res.size() <= 1 )
				{
				res = getMorphInfo( *LemmatizerRussian, *RusGramTab, s );
				if( res.size() <= 1 ) return "";
				}
			}
		}

	for( p = res.begin(); p != res.end(); p++ )
		str += " " + *p;
  	return str;
   }


Morphology::~Morphology()
   {
   delete LemmatizerRussian;
   delete RusGramTab;
   delete LemmatizerEnglish;
   delete EngGramTab;
   delete LemmatizerGerman;
   delete GerGramTab;
   }
